package com.jxnu.escms.Service.UserService.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxnu.escms.Dao.UserDao.UserDao;
import com.jxnu.escms.Pojo.PageResult;
import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.UserService.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public User containUserByNameAndPassword(String username, String password) {
        return userDao.getUserByNameAndPass(username, password);
    }

    @Override
    public User containUserByEmailAndPassword(String email, String password) {
        return userDao.getUserByEmailAndPass(email, password);
    }

    @Override
    public User containUserByNameAndEmail(String username, String email) {
        return userDao.getAllByEmailAndName(username, email);
    }

    @Override
    public int updateDate(String timestamp, Integer id) {
        return userDao.updateTime(timestamp, id);
    }

    @Override
    public int registerUser(Map<String, String> input) {
        String email = input.get("email");
        String username =input.get("username");
        String password = input.get("password");
        return userDao.insertUser(username, email, password, "住户");
    }

    @Override
    public int updatePassword(String newPassword, Integer id) {
        return userDao.updatePass(newPassword, id);
    }

    @Override
    public int changePassword(String name, String email, String newPassword) {
        return userDao.updatePasswordByNameAndEmail(name, email, newPassword);
    }

    @Override
    public Map<String, Object> getUserAddress(Integer id) {
        return userDao.getUserAddress(id);
    }

    @Override
    public User getUserByID(int id){
        return userDao.getUserById(id);
    }

    @Override
    public int changeUserMes(User user) {
        return userDao.changeUserMessage(user);
    }

    @Override
    public int updateUserAddress(List address, int id) {
        String com = (String) address.get(0);
        String build = (String) address.get(1);
        String nuit = (String) address.get(2);
        String door = (String) address.get(3);

        // 判断是否存在com这个社区
        Map c = userDao.containCom(com);
        if(c == null){
            // 添加社区
            userDao.insertCom(com);
            c = userDao.containCom(com);
        }
        // 得到社区对应的编号
        int com_id = (int) c.get("com_id");

        // 修改用户地址
        return userDao.updateAddressInt(id, com_id, build, nuit, door);
    }

    /**
     * @param start 当前页
     * @param pageSize 查询的行数
     * @return 查询或的结果
     */
    @Override
    public PageResult<User> getUsers(Integer start, Integer pageSize) {
        log.info("分页查询：start:{}， pageSize:{}", start, pageSize);
        log.info("开启pageHelper功能");
        // 使用pageHelper
        Page<Object> page = PageHelper.startPage(start, pageSize);
        System.out.println(page);
        //获取用户信息,如果不加下面这行会出现total为0
        Page<User> pages = (Page<User>) userDao.getAll();
        return new PageResult<User>(Math.toIntExact(pages.getTotal()), pages);
    }

    @Override
    public String getUserHealthyCode(int id) {
        return userDao.getHealthyCode(id);
    }

    @Override
    public int userCharge(int admin, int user, String controlType) {
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleFormatter.format(new Date());
        return userDao.operaUser(admin, user, controlType, date);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public User getUserByAAndN(String account, String realName) {
        if(Objects.equals(account, "")){
            account = null;
        }
        if(Objects.equals(realName, "")){
            realName = null;
        }
        return userDao.getUserByRealAndAccount(account, realName);
    }

    @Override
    public int addUser(User user) {
        return userDao.addUser(user);
    }

    @Override
    public Map containCom(String name) {
        return userDao.containCom(name);
    }

    @Override
    public int insertCom(String name) {
        return userDao.insertCom(name);
    }

    @Override
    public int addAddress(int user_id, int com_id, String build, String nuit, String door) {
        return userDao.addAddress(user_id, com_id, build, nuit, door);
    }

    @Override
    public PageResult<User> getDangerUsers(Integer start, Integer pageSize) {
        // 使用pageHelper
        Page<Object> page = PageHelper.startPage(start, pageSize);
        System.out.println(page);
        //获取用户信息,如果不加下面这行会出现total为0
        Page<User> pages = (Page<User>) userDao.getAllDanger();
        return new PageResult<User>(Math.toIntExact(pages.getTotal()), pages);
    }

}
